<?php

namespace app\api\controller;

use app\api\model\Ocr;
use app\common\controller\Api;
use app\common\model\Category;
use app\common\model\User;
use think\Db;
use think\Exception;

/**
 * 首页接口
 */
class Shiti extends Api
{
    protected $noNeedLogin = ['recognitionImg','ceshi','print'];
    protected $noNeedRight = ['*'];

    public function ceshi()
    {

    }

    /**
     * 搜索试题
     *
     */
    public function searchShiti()
    {
        try {
            $keyword = input('keyword','');
            $subject = input('subject','');
            $next = input('next',0);
            $model = new \app\api\model\Shiti();
            $res = $model->search($keyword,$subject,$this->auth->id,$next);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('请求成功',$res);
    }

    //根据试题查答案
    public function shitiAnswer()
    {
        $md52 = input('md52','');
        $model = new \app\api\model\Shiti();
        $res = $model->answer($md52);

        $this->success('请求成功',$res);
    }

    //识别图片题目
    public function recognitionImg()
    {
        $img = input('img','');

        $res  = Ocr::ceshi($img);

        $this->success('请求成功',$res);
    }

    //能否下一步
    public function canNext()
    {
        $next = input('next',0);

        $shiti_model = new \app\api\model\Shiti();
        $user = User::get($this->auth->id);
        if ($user->category_id){
            $category = Category::get($user->category_id);
            $category_name = $category->getAttr('name');
            // 直接在array_keys中使用array_filter
            //$searchTerm = substr($category_name,0,9);
            $searchTerm = $category_name;

            $results = array_filter(array_map(function($item) use ($searchTerm) {
                return [
                    'id'=>strtolower($item['id']),
                    'name' => strtolower($item['name']),
                    'level' => strtolower($item['level'])
                ]; // 转换为小写以便于搜索
            }, $shiti_model->grade), function($item) use ($searchTerm) {
                return strpos($item['name'], $searchTerm) !== false; // 搜索小写后的字符串
            });

            if (count($results)>0){
                $gradeId = array_values($results)[0]['id'];
                $level = array_values($results)[0]['level'];

                $key = 0;

                if ($next==0){
                    $param['gradeId'] = $gradeId;
                } else {
                    foreach ($shiti_model->grade as $k=>$v){
                        if ($v['id']==$gradeId){
                            //$next_gradeId = $v['id'];
                            $key = $k;
                            break;
                        }
                    }
                    if ($key>1){
                        $next_gradeId = $shiti_model->grade[$key-$next]['id'];
                        $next_level = $shiti_model->grade[$key-$next]['level'];
                        //dump($level);
                        //dump($next_level);
                        if ($level<$next_level){
                            $this->success('请求成功',1);
                        }
                        $param['gradeId'] = $next_gradeId;
                    }

                }
            }

        }
        $this->success('请求成功',0);
    }

    public function print()
    {
        //$subject = input('subject','yuwen');
        $topic = input('topic',1);
        $answer = input('answer',1);
        $ids = input('ids','');
        $title = input('title','');
        $surplus = input('surplus','');

        $model = new \app\api\model\Shiti();
        $res = $model->print($ids,$topic,$answer,$title,$surplus);

        $this->success('请求成功',$res);
    }
}
